#include <iostream>
#include <fstream>

#include "global.h"
#include "Instance.h"
#include "State.h"
#include "Solver.h"

#include <boost/mpi.hpp>
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
namespace mpi = boost::mpi;

using namespace std;

inline void printUsage() {
  cout << "x36par - mbg" << endl;
  cout << "Usage: main <filename>" << endl;
}

int main(int argc, char* argv[]) {
  mpi::environment kEnv(argc, argv);
  mpi::communicator kWorld;

  if (kWorld.rank() == 0) {
    if (argc < 2) {
      printUsage();
      return ( 1 );
    }
    fstream kFile;
    kFile.open(argv[1]);
    if (!kFile || kFile.eof()) {
      cerr << "File not found or empty" << endl;
      return ( 1 );
    }

    try {
      kFile >> gInstance;
    } catch (const char* e)  {
      cerr << "ERROR: Process " << kWorld.rank() << ": " << e << endl;
      return ( 1 );
    }
  }    

  Solver kSolver;

  try {
    kSolver.solve();
  } catch (const char* e) {
    cerr << "ERROR: Process " << kWorld.rank() << ": " << e << endl;
    return 1;
  }

  if (kWorld.rank() == 0) {
    cout << "FOUND\t" << kSolver.getSolution();
  }
  
  cout << kWorld.rank() << "\t" << kSolver.getElapsed() << " seconds" << endl
       << "\t" << kSolver.getCount() << " states" << endl
       << "\t" << kSolver.getCount()/kSolver.getElapsed() << " states/second" << endl;

#ifdef DEBUG
  cout << "Jste strasni kokoti!" << endl;
#endif
  return ( 0 );
}
// vim: ts=2 fdm=marker syntax=cpp expandtab sw=2
